from rest_framework.permissions import IsAdminUser
from rest_framework.views import APIView
from users.models import User
from rest_framework.response import Response
from datetime import date, timedelta
from goods.models import GoodsVisitCount
from meiduo_admin.serializers.increments import GoodsVisitCountSerializer


class IncrementTotalcountView(APIView):
    """
    注册用户总量统计
    """
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        now_date = date.today()
        count = User.objects.all().count()
        data = {
            'count': count,
            'date': now_date
        }
        return Response(data)


class IncrementDayCountView(APIView):
    """
    获取当天注册用户总量统计
    """
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取当日注册用户数量 date_joined 记录创建账户时间
        count = User.objects.filter(date_joined__gte=now_date).count()
        data = {
            'count': count,
            'date': now_date
        }
        return Response(data)


class IncrementDayActiveView(APIView):
    """
    获取当天登录用户总量统计
    """
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        now_date = date.today()
        count = User.objects.filter(last_login__gte=now_date).count()
        data = {
            'count': count,
            'date': now_date
        }
        return Response(data)


class IncrementDayOrderView(APIView):
    """
    获取当天下单用户总量统计(第一种方法,待会回来搞第二种方法)
    """
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        now_date = date.today()
        # 获取当日下单用户数量  orders__create_time 订单创建时间
        count = User.objects.filter(orders__create_time__gte=now_date).count()
        data = {
            'count': count,
            'date': now_date
        }
        return Response(data)


class IncrementMonthCountView(APIView):
    """
        获取单月每天注册用户数量统计
    """
    # 指定权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1、获取当前日期
        now_date = date.today()
        # 2、获取一个月前的日期
        start_date = now_date - timedelta(29)
        # 获取每天的注册用户数量
        data_list = []
        for i in range(30):
            # 获取起始时间
            index_date = start_date + timedelta(i)
            # 获取第二天实现
            next_date = index_date + timedelta(1)
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=next_date).count()
            data_list.append({
                'count': count,
                'date': start_date
            })
            # start_date = next_date
        return Response(data_list)


class GoodsVisitCountView(APIView):
    """
        获取当天分类数量统计
    """
    # 指定权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        now_date = date.today()
        goods = GoodsVisitCount.objects.filter(date__gte=now_date)
        ser = GoodsVisitCountSerializer(goods, many=True)
        return Response(ser.data)
